﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class TabController : MonoBehaviour
{
    [System.Serializable]
    public struct TabItem
    {
        public Button TabButton;
        public string TabPanelPath;
        [HideInInspector]
        public GameObject TabPanel;
    }

    public Transform TabPanelParent;
    public TabItem[] tabItems;

    public void Awake()
    {
        for (int i = 0; i < tabItems.Length; i++)
        {            
            tabItems[i].TabPanel = Singleton<UIManager>.GetInstance().GetSingleUI(new UIType(tabItems[i].TabPanelPath, TabPanelParent));
            if (tabItems[i].TabPanel.GetComponent<BaseCG>() == null)
                tabItems[i].TabPanel.AddComponent<BaseCG>();
            if (i != 0)
                tabItems[i].TabPanel.GetComponent<BaseCG>().Hide();
        }
        foreach (var item in tabItems)
        {
            item.TabButton.onClick.AddListener(() => {
                for (int i = 0; i < tabItems.Length; i++)
                {
                    if (tabItems[i].Equals(item))
                        tabItems[i].TabPanel.GetComponent<BaseCG>().Show();
                    else
                        tabItems[i].TabPanel.GetComponent<BaseCG>().Hide();
                }
            });
        }
    }
}
